package com.eystar.pdc.directive;

import com.eystar.pdc.model.DicItem;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.ParseException;
import com.jfinal.template.stat.Scope;

import cn.hutool.core.util.StrUtil;

/**
 * 获取item中的值
 * #dic_item_get("dic_code","item_code")
 * @author Administrator
 *
 */
public class DicItemGetDirective extends Directive {
	
	public void setExprList(ExprList exprList) {
		int len = exprList.length();
		if (len != 2) {
			throw new ParseException("指令#dic_item_get必须有两个参数 code和item", location);
		}
		super.setExprList(exprList);
	}

	@Override
	public void exec(Env env, Scope scope, Writer writer) {
		String code = (String) exprList.getFirstExpr().eval(scope);
		String item = (String) exprList.getLastExpr().eval(scope);
		String value = DicItem.dao.findDicItemValue(code, item);
		write(writer, StrUtil.isBlank(value) ? "" : value);
	}

}
